//
//  becaredb.m
//  tcpoctest
//
//  Created by apple on 13-3-15.
//  Copyright (c) 2013年 gump. All rights reserved.
//

#import "becaredb.h"
#import "CareDataSource.h"

@implementation becaredb

-(void)creatTable
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    [db executeUpdate:@"create table if not exists becareperson(mail text,firendmail text)"];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)initData
{
    if (db == nil) {
        return;
    }
    
    NSString *str = [NSString stringWithFormat:@"select firendmail from becareperson where mail = '%@'",
                     [DataControl shareControl].personDS.uinfo.mail];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    FMResultSet *s = [db executeQuery:str];
    
    [[DataControl shareControl].becareds RemoveAllShowArr];
    
    while ([s next])
    {
        [[DataControl shareControl].becareds AddShowArrObject:[s stringForColumn:@"firendmail"]];
    }
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)insertBeCare:(NSString *)mail
{
    if (db == nil) {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:
                        @"insert into becareperson (mail,firendmail) values('%@','%@')"
                        ,[DataControl shareControl].personDS.uinfo.mail
                        ,mail];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    [db executeUpdate:strsql];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)deleteAll
{
    if (db == nil) {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:
                        @"DELETE FROM becareperson WHERE mail = '%@'"
                        ,[DataControl shareControl].personDS.uinfo.mail];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    [db executeUpdate:strsql];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

@end
